package com.vancouver.knows.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.astuetz.PagerSlidingTabStrip;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;

public class MyFollowedActivity extends BaseActivity {

	private Context mContext;
	@Bind(R.id.text_tips)
	TextView mTextTips;
	@Bind(R.id.tabStrip)
	PagerSlidingTabStrip indicator;
	@Bind(R.id.pager)
	ViewPager viewPager;

	private String[] titles;
	private int pageNum;
	private FollowedPagerAdapter pagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_fllowed);
		mContext = MyFollowedActivity.this;
		setViews();
	}

	private void setViews() {
		Intent intent = getIntent();
		mTextTips.setText(intent.getStringExtra("MY_TITLE"));
		titles = getResources().getStringArray(R.array.my_followed_array);
		pageNum = titles.length;
		FragmentManager fm = getSupportFragmentManager();
		pagerAdapter = new FollowedPagerAdapter(fm);
		viewPager.setAdapter(pagerAdapter);
		indicator.setViewPager(viewPager);

		int tabIndex = intent.getIntExtra("TAB_INDEX", -1);
		if (tabIndex != -1) {
			viewPager.setCurrentItem(tabIndex);
		}
	}

	@OnClick(R.id.cancel)
	public void onCommonCancelClick() {
		finish();
	}

	public class FollowedPagerAdapter extends FragmentStatePagerAdapter {

		public FollowedPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return createFragment(position);
		}

		@Override
		public int getCount() {
			return pageNum;
		}

		private Fragment createFragment(int position) {
			int type = FollowedChildrenFragment.FOLLOWED_TYPE_ALL;
			switch (position) {
			case 0:
				type = FollowedChildrenFragment.FOLLOWED_TYPE_ALL;
				break;
			case 1:
				type = FollowedChildrenFragment.FOLLOWED_TYPE_CITY_NEWS;
				break;
			case 2:
				type = FollowedChildrenFragment.FOLLOWED_TYPE_EVENTS;
				break;
			case 3:
				type = FollowedChildrenFragment.FOLLOWED_TYPE_SHOP;
				break;
			}

			return FollowedChildrenFragment.newInstance(type);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}
	}

}